//
//  CarPathController.m
//  BalanceCar
//
//  Created by 郑信鸿 on 17/6/20.
//  Copyright © 2017年 郑信鸿. All rights reserved.
//

#import "CarPathController.h"
#import <MAMapKit/MAMapKit.h>
#import <AMapFoundationKit/AMapFoundationKit.h>
#import <AMapLocationKit/AMapLocationKit.h>
#import "RunLocationModel.h"
#import "TrackMananger.h"

@interface CarPathController ()<MAMapViewDelegate,AMapLocationManagerDelegate>

@property(nonatomic, strong)MAMapView *mapView;

@property(nonatomic, assign)int endIndexPath;

@property(nonatomic, strong)RunLocationModel *endLocation;

@property(nonatomic, strong)NSMutableArray *lineArray;

@end

@implementation CarPathController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"车辆轨迹";
    // Do any additional setup after loading the view.
    ///地图需要v4.5.0及以上版本才必须要打开此选项（v4.5.0以下版本，需要手动配置info.plist）
    [AMapServices sharedServices].enableHTTPS = YES;
    
    
    ///把地图添加至view
    [self.view addSubview:self.mapView];
    
   // self.endLocation = 
    
    [self mapAddCommonPolyline];
}


//- (MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id <MAOverlay>)overlay
//{
//    if ([overlay isKindOfClass:[MAPolyline class]])
//    {
//        MAPolylineRenderer *polylineRenderer = [[MAPolylineRenderer alloc] initWithPolyline:overlay];
//
//        polylineRenderer.lineWidth    = 8.f;
//        polylineRenderer.strokeColor  = [UIColor colorWithRed:0 green:1 blue:0 alpha:0.6];
//        polylineRenderer.lineJoin = kCGLineJoinRound;
//        polylineRenderer.lineCap  = kCGLineCapRound;
//
//        return polylineRenderer;
//    }
//    return nil;
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



-(MAMapView *)mapView{
    
    if (_mapView == nil) {
        
        ///初始化地图
        _mapView = [[MAMapView alloc] initWithFrame:self.view.bounds];
        _mapView.desiredAccuracy = kCLLocationAccuracyBest;
        _mapView.distanceFilter = 1.0f;
        _mapView.showsUserLocation = YES;
        _mapView.userTrackingMode = MAUserTrackingModeFollow;//地图跟着位置移动
        MAUserLocationRepresentation *r = [[MAUserLocationRepresentation alloc] init];
        r.showsAccuracyRing = NO;///精度圈是否显示，默认YES
        r.showsHeadingIndicator = YES;
        [_mapView updateUserLocationRepresentation:r];
        _mapView.zoomLevel = 16;
        _mapView.maxZoomLevel = 18;
        //不显示比例尺
        _mapView.showsScale =NO;
        //不显示罗盘
        _mapView.showsCompass = NO;
        _mapView.delegate  = self;
    }
    
    return _mapView;
}


////根据头部信息显示方向
//-(void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation{
//
//    if(nil == userLocation || nil == userLocation.heading
//       || userLocation.heading.headingAccuracy < 0) {
//        return;
//    }
//
//    CLLocationDirection  theHeading = userLocation.heading.magneticHeading;
//
//    float direction = theHeading;
//
////    if(nil != _myLocationAnnotationView) {
////        if (direction > 180)
////        {
////            direction = 360 - direction;
////        }
////        else
////        {
////            direction = 0 - direction;
////        }
////        _myLocationAnnotationView.image = [self.myLocationImage imageRotatedByDegrees:-direction];
////    }
//
//}

-(void)mapAddCommonPolyline{
    
    for (int i = _endIndexPath; i< [TrackMananger shareManager].finishLocationArray.count; i++) {
        
        RunLocationModel *newlocation = [TrackMananger shareManager].finishLocationArray[i];
        MAMapPoint point1 = MAMapPointForCoordinate(newlocation.location);
        MAMapPoint point2 = MAMapPointForCoordinate(_endLocation.location);
        
        //2.计算距离
        CLLocationDistance newDistance = MAMetersBetweenMapPoints(point1,point2);
        if (newDistance>10) {
            
            CLLocationCoordinate2D commonPolylineCoords[2];
            commonPolylineCoords[0] = newlocation.location;
            commonPolylineCoords[1] = _endLocation.location;
            
            MAPolyline *commonPolyline = [MAPolyline polylineWithCoordinates:commonPolylineCoords count:2];
            
            [_lineArray addObject:commonPolyline];
            [self.mapView addOverlay: commonPolyline];
            
            _endIndexPath = i;
            _endLocation = newlocation;
        }
    }
}

- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
